﻿using System;
using System.Data;
using System.Collections.Generic;
using SERP.DAO;
namespace SERP.Controllers
{
    public class OrganizationsController : OrganizationsControllerBase
    {
        public OrganizationsController()
            : base()
        {
        }
        public static OrganizationsEntity GetOrganizationByText(string text)
        {
            OrganizationsEntity o = GetOrganizationByCode(text);
            if (o == null)
                o = GetOrganizationByName(text);
            return o;
        }
        public static OrganizationsEntity GetOrganizationByCode(string code)
        {
            return ListOrganizationsStatic.Find(delegate(OrganizationsEntity o) { return Commons.Common.Convert_ASC(o.OrgCode) == Commons.Common.Convert_ASC(code); });

        }
        public static OrganizationsEntity GetOrganizationByName(string name)
        {
            return ListOrganizationsStatic.Find(delegate(OrganizationsEntity o) { return Commons.Common.Convert_ASC(o.OrgName) == Commons.Common.Convert_ASC(name); });
        }
        public static OrganizationsEntity GetOrganizationById(int organizationId)
        {
            return ListOrganizationsStatic.Find(delegate(OrganizationsEntity o) { return o.OrganizationId == organizationId; });

        }
        public static List<OrganizationsEntity> GetOrganizationByParent(int parentId)
        {
            return ListOrganizationsStatic.FindAll(delegate(OrganizationsEntity o) { return o.ParentId == parentId; });
        }
        private DataTable _Table;
        private OrganizationsEntity oOrganizations;
        public OrganizationsEntity OrganizationsObject
        {
            get
            {
                return oOrganizations;
            }
            set
            {
                oOrganizations = value;
            }
        }
        public DataTable Table
        {
            get
            {
                if (_Table == null && AllowCaching)
                {
                    _Table = Cache.Get<DataTable>(CACHE_KEY_DS);
                }
                return _Table;
            }
            set
            {
                _Table = value;
            }
        }
        public void LoadAllDSOrganizations(bool clearCache)
        {
            bool bForce = clearCache;
            if (bForce || _Table == null)
            {
                DataSet ds = Service.SelectAllDS();
                _Table = ds.Tables[0];
                AddIndexToTable(_Table);
                if (AllowCaching)
                    AddCache(CACHE_KEY_DS, _Table);
            }
        }
        public void AddIndexToTable()
        {
            AddIndexToTable(_Table);
        }
        public void ResetTableIndex()
        {
            ResetTableIndex(_Table);
        }
        public void LoadObject(Int32 OrganizationId)
        {
            OrganizationsEntity oLoad = FindOrganizations(OrganizationId);
            if (oLoad == null)
            {
                LoadAllOrganizations(true);
                oOrganizations = FindOrganizations(OrganizationId);
            }
            else
            {
                oOrganizations = oLoad;
            }
        }
        public bool AddNew()
        {
            if (oOrganizations == null) return false;
            bool result = Service.Save(oOrganizations);
            if (result)
            {
                AddObjectToCache(oOrganizations);
            }
            return result;
        }
        public bool Update()
        {
            if (oOrganizations == null) return false;
            return Service.Update(oOrganizations);
        }
        public bool Delete()
        {
            if (oOrganizations == null) return false;
            bool retvalue = Service.Delete(oOrganizations);
            if (retvalue)
            {
                RemoveObjectFromCache(oOrganizations);
            }
            return retvalue;
        }
        public bool DeleteByID(Int32 OrganizationId)
        {
            bool retvalue = Service.DeleteByID(OrganizationId);
            if (retvalue)
            {
                OrganizationsEntity oDeleted = FindOrganizations(OrganizationId);
                RemoveObjectFromCache(oDeleted);
            }
            return retvalue;
        }
        public void ResetObject()
        {
            this.oOrganizations = new OrganizationsEntity();
        }
        public void MapObjectToDataRow(DataRow row)
        {
            this.MapObjectToDataRow(this.OrganizationsObject, row);
        }
        public OrganizationsEntity LoadOrganizations(Int32 OrganizationId)
        {
            OrganizationsEntity o = Service.SelectOne(OrganizationId);
            return o;
        }
        #region Cached
        private static List<OrganizationsEntity> _ListOrganizations;
        private static bool _AllowCaching = true;
        public static bool AllowCaching
        {
            get { return _AllowCaching; }
            set { AllowCaching = value; }
        }
        public static void ClearCache()
        {
            if (!_AllowCaching) return;
            Cache.Remove(CACHE_KEY_DS);
            Cache.Remove(CACHE_KEY_LS);
        }
        private const string CACHE_KEY_DS = "OrganizationsDS";
        private const string CACHE_KEY_LS = "OrganizationsLS";
        private void AddObjectToCache(OrganizationsEntity newObject)
        {
            if (!AllowCaching) return;
            GetCacheList().Add(newObject);
        }
        private void RemoveObjectFromCache(OrganizationsEntity oRemove)
        {
            if (!AllowCaching) return;
            if (oRemove != null)
                GetCacheList().Remove(oRemove);
        }
        private static List<OrganizationsEntity> GetCacheList()
        {
            if (Cache == null) return default(List<OrganizationsEntity>);
            List<OrganizationsEntity> ls = Cache.Get<List<OrganizationsEntity>>(CACHE_KEY_LS);
            if (ls == null)
            {
                ls = new List<OrganizationsEntity>();
                AddCache(CACHE_KEY_LS, ls);
            }
            return ls;
        }
        public static Dictionary<Int32, OrganizationsEntity> GetOrganizationsDictionary()
        {
            Dictionary<Int32, OrganizationsEntity> dic = new Dictionary<Int32, OrganizationsEntity>();
            foreach (OrganizationsEntity o in ListOrganizationsStatic)
            {
                dic.Add(o.OrganizationId, o);
            }
            return dic;
        }
        public List<OrganizationsEntity> ListOrganizations
        {
            get
            {
                return LoadAllOrganizations(false);
            }
        }
        public static List<OrganizationsEntity> ListOrganizationsStatic
        {
            get
            {
                LoadAllOrganizations(false);
                return _ListOrganizations;
            }
        }
        public OrganizationsEntity FindOrganizations(Int32 OrganizationId)
        {
            if (AllowCaching)
            {
                _ListOrganizations = GetCacheList();
            }
            OrganizationsEntity oResult = _ListOrganizations.Find(delegate(OrganizationsEntity oFind)
            {
                bool result = true;
                result &= oFind.OrganizationId.Equals(OrganizationId);
                return result;
            });
            return oResult;
        }
        public static List<OrganizationsEntity> LoadAllOrganizations(bool clearCache)
        {
            _ListOrganizations = new List<OrganizationsEntity>();
            if (AllowCaching)
            {
                _ListOrganizations = GetCacheList();
            }
            if (_ListOrganizations == null || _ListOrganizations.Count == 0)
                clearCache = true;
            if (!clearCache) return _ListOrganizations;
            _ListOrganizations = new List<OrganizationsEntity>(Service.SelectAll());
            if (AllowCaching)
                AddCache(CACHE_KEY_LS, _ListOrganizations);
            return _ListOrganizations;
        }
        #endregion
    }
}
